<?php
namespace app\backend\controller;
use app\backend\model\ProductModel;
use app\backend\model\ProductSpecModel;
use app\backend\model\UserModel;
use app\common\model\UserBill;

/**
 * 登录类
 * @author Ray 541720500@qq.com
 */
class User extends Base
{
    public function index()
    {
        $where = [];
        if(input('name')) {
            array_push($where, ['name', 'like', '%'.input('nickname').'%']);
        }
        if(input('mob')) {
            array_push($where, ['mobile', '=', input('mob') ]);
        }
        $this->assign('data', [
            'title' => '会员列表',
            'collection' => UserModel::where($where)->order('id desc')->paginate(config('paginate.per_page')),
            'thead' => [
                ['#','width:60px'],
                '称呼',
                '手机号',
                '最后登录',
            ],
            'fields' => [
                function($row) {
                    return '<image src="'.$row['avatar'].'" class="image-in-td">';
                },
                'nickname','mobile','last_time',
            ],
            'buttons' => [
                ['title'=>'详情', 'onclick' => 'showBigWindow', 'url'=> url('details')], // 'onclick'=>'redirect',
//                ['title'=>'改状态','onclick'=>'ajaxReq','url'=> url('switchStatus')],
            ],
            'searcher' => [
                ['type'=>'input','name'=>'name','placeholder'=>'称呼'],
                ['type'=>'input','name'=>'mob','placeholder'=>'手机号'],
//                ['type'=>'select','name'=>'status', 'option' => config('status.normal')],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }

    public function bill()
    {
        $where = [];
        if(input('nickname')) {
            $uid = \app\common\model\User::where('nickname',input('nickname') )->value('id');
            array_push($where, ['uid', '=', $uid]);
        }
        if(input('mob')) {
            array_push($where, ['mobile', '=', input('mob') ]);
        }
        $this->assign('data', [
            'title' => '会员账单',
            'collection' => UserBill::where($where)->with('user')->order('id desc')->paginate(config('paginate.per_page')),
            'thead' => [
                ['时间','width:200px'],
                '会员',
                '金额',
                '备注',
            ],
            'fields' => ['add_time',
                function($row) {
                    return $row->user->nickname;
                },
                'amount','remark',
            ],
            'searcher' => [
                ['type'=>'input','name'=>'nickname','placeholder'=>'会员昵称'],
//                ['type'=>'input','name'=>'mob','placeholder'=>'手机号'],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }
    public function details()
    {
        $id = input('id');
        $user = \app\common\model\User::find($id);
        $info = [
            "UID" => $user->id,
            "称呼" => $user->name ,
            "电话" => $user->mobile ,
            "最后登录时间" => $user->last_time ,
            "最后登录IP" => $user->last_ip ,
            "加入时间" => $user->add_time ,
        ];
        return $this->fetch('public/details_builder', ['data' => [
            'title' => '会员详情',
            'info' => $info,
        ]]);
    }
}
